package com.qiguliuxing.dts.wx.config;

import java.util.List;

import com.qiguliuxing.dts.wx.annotation.support.CacheRequestBodyFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.qiguliuxing.dts.wx.annotation.support.LoginUserHandlerMethodArgumentResolver;

import javax.servlet.Filter;

@Configuration
public class WxWebMvcConfiguration implements WebMvcConfigurer {
	private static final Logger logger = LoggerFactory.getLogger(WxWebMvcConfiguration.class);

	@Override
	public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
		argumentResolvers.add(new LoginUserHandlerMethodArgumentResolver());
	}

	/**
	 * 创建 RequestBodyCacheFilter Bean，可重复读取请求内容
	 */
	@Bean
	public FilterRegistrationBean<CacheRequestBodyFilter> requestBodyCacheFilter() {
		return createFilterBean(new CacheRequestBodyFilter(), 1);
	}

	public static <T extends Filter> FilterRegistrationBean<T> createFilterBean(T filter, Integer order) {
		FilterRegistrationBean<T> bean = new FilterRegistrationBean<>(filter);
		bean.setOrder(order);
		// 拦截所有资源
		bean.addUrlPatterns("/*");
		return bean;
	}
}
